﻿using DFGK.IService;
using DFGK.Service;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Dto;
using DFGK_WMS.Model.Enum;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace DFGK_WMS.WebAPI.Controllers.PC.库存管理
{
    /// <summary>
    /// 库存调整
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiExplorerSettings(GroupName = "PC")]
    [ApiController]
    public class InventoryAdjustController : BaseController
    {
        private readonly IInventoryAdjustService inventoryAdjustService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="environment"></param>
        /// <param name="inventoryAdjustService"></param>
        public InventoryAdjustController(IWebHostEnvironment environment, IInventoryAdjustService inventoryAdjustService) : base(environment)
        {
            this.inventoryAdjustService = inventoryAdjustService;   
        }

        /// <summary>
        /// 库存调整添加
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddInventoryAdjust(InventoryAdjust m)
        {
            string message = "";

            if (m.Qty==null)
            {
                return Ok(BaseResult.Result("400", "调整数量不能为空！"));
            }
            m.CreateUserId = base.BaseGetNowUserId();
            m.CreateUserName = base.BaseGetNowUserName();
            bool result = inventoryAdjustService.AddInventoryAdjust(m, out message);
            if (result)
            {
                return Ok(BaseResult.Result(result, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(result, "添加失败！" + message, "500"));
        }

        /// <summary>
        /// 库存调整添加（南宁现用）
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddInventoryAdjustList(List<InventoryAdjust> list)
        {
            string message = "";
            int count = list.Count;
            if (count==0)
                return Ok(BaseResult.Result("400", "数据不能为空！"));      
            else if(count == 2 && list.First().SourceType != 20)
                return Ok(BaseResult.Result("400", "请选择减出的库存数据！"));
            list.First().CreateUserId = base.BaseGetNowUserId();
            list.First().CreateUserName = base.BaseGetNowUserName();
            bool result = inventoryAdjustService.AddInventoryAdjustList(list, out message);
            if (result)
            {
                return Ok(BaseResult.Result(result, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(result, "添加失败！" + message, "500"));
        }

        /// <summary>
        /// 获取库存调整列表
        /// </summary>
        /// <param name="order"></param>
        /// <param name="type"></param>
        /// <param name="status"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetInventoryAdjustList(string? order,int type, int status, int pageIndex = 1, int pageSize = 20)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;

            var list = inventoryAdjustService.GetInventoryAdjustList(order, type,status, pageIndex, pageSize, out totalCount);

            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }
        /// <summary>
        /// 删除库存调整
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelInventoryAdjust(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }

            bool IsOK = inventoryAdjustService.DelInventoryAdjust(id);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！", "500"));
        }

        /// <summary>
        /// 根据id获取信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetInventoryAdjustById(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var obj = inventoryAdjustService.GetInventoryAdjustById(id);
            if (obj != null)
            {
                return Ok(BaseResult.Result(obj, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));

        }

        /// <summary>
        /// 根据id获取集合（南宁现用）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetInventoryAdjustListById(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var list = inventoryAdjustService.GetInventoryAdjustListById(id);
            return Ok(BaseResult.Result(list, "查询成功！", "200", 0));
        }

        /// <summary>
        /// 库存调整修改
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdInventoryAdjust(InventoryAdjust m)
        {
            string message = "";

            if (m.Qty == null)
            {
                return Ok(BaseResult.Result("400", "调整数量不能为空！"));
            }
            m.CreateUserId = base.BaseGetNowUserId();
            m.CreateUserName = base.BaseGetNowUserName();
            bool result = inventoryAdjustService.UpdInventoryAdjust(m, out message);
            if (result)
            {
                return Ok(BaseResult.Result(result, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(result, "添加失败！" + message, "500"));
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdInventoryAdjustStatus(AuditDto m)
        {
            if (m.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            string message;
            bool IsOK = inventoryAdjustService.UpdInventoryAdjustStatus(m,out message);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "修改成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "修改失败！"+message, "500"));
        }
    }
}
